package com.peng.redis;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * JedisWithZSetSample
 *
 * @author lupeng
 * Created on 2022-05-06
 */
public class JedisWithZSetSample {

    private static final Logger logger = LoggerFactory.getLogger(JedisWithStringSample.class);

    private JedisPool pool;

    public static final String KEY = "zsetkey";
    public static final String KEY1 = "zsetkey1";

    @BeforeEach
    public void init() {
        // pool = new JedisPool("localhost", 6379);
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(1000);
        poolConfig.setMaxIdle(32);
        poolConfig.setTestOnBorrow(true);

        pool = new JedisPool(poolConfig, "test-zhaopin05.rdb.58dns.org", 50090, 1000, "06643b881e175041");
        logger.info("JedisPool create success");
    }

    @AfterEach
    public void destroy() {
        if (pool != null) {
            pool.close();
        }
    }

    @Test
    public void s1() {
        try (Jedis jedis = pool.getResource()) {
            for (int i = 0; i < 100; i++) {
                jedis.zadd(KEY, i, i + "-member");
            }
            logger.info("zcard {}", jedis.zcard(KEY));
        }
    }

    @Test
    public void s2() {
        try (Jedis jedis = pool.getResource()) {
            for (int i = 0; i < 100; i++) {
                Double zscore = jedis.zscore(KEY, i + "-member");
                logger.info("zscore {}", zscore);
            }
        }
    }

    /**
     * zscore 查询member的分数
     * zincrby 给member加分
     * zcount 查询指定分数范围的元素个数
     */
    @Test
    public void s3() {
        try (Jedis jedis = pool.getResource()) {
            String member = "0-member";
            logger.info(jedis.zscore(KEY, member) + "");
            double zincrby = jedis.zincrby(KEY, 1, member);
            logger.info("zscore {}", zincrby);

            logger.info("zcount {}", jedis.zcount(KEY, 10, 80));
        }
    }

    /**
     * zscore 查询member的分数
     * zincrby 给member加分
     * zcount 查询指定分数范围的元素个数
     */
    @Test
    public void s4() {
        try (Jedis jedis = pool.getResource()) {
            String member = "0-member";
            logger.info(jedis.zscore(KEY, member) + "");
            double zincrby = jedis.zincrby(KEY, 1, member);
            logger.info("zscore {}", zincrby);

            logger.info("zcount {}", jedis.zcount(KEY, 10, 80));
        }
    }

    @Test
    public void t5() {
        try (Jedis jedis = pool.getResource()) {
            System.out.println(jedis.zrank("testlupeng10", "b"));
            System.out.println(jedis.zrank("testlupeng10", "e"));
        }
    }

}
